// 封装购物车相关的逻辑
import { defineStore } from "pinia"
import { ref, computed } from "vue"
import { useUserStore } from "./user"
import { addCartAPI, getCartListAPI, delCartAPI, mergeCartAPI } from "@/apis/cart"

export const useCartStore = defineStore('cart', () => {
  const userStore = useUserStore()
  const isLogin = computed(() => userStore.userInfo.token)
  // 1.定义与购物车相关的state
  const cartList = ref([])
  // 2.定义与购物车相关的action
  // 将获取最新的购物车信息抽象为一个函数
  const getNewCartList = async () => {
    const res = await getCartListAPI()
    cartList.value = res.result
  }
  // 添加到购物车
  const addCart = async (goods) => {
    if(isLogin.value) {
      // 判断当前用户为登录，调用接口
      await addCartAPI({
        skuId: goods.skuId,
        count: goods.count
      })
      // 获取最新的购物车列表
      getNewCartList()
    } else {
      // 1.判断当前添加的商品是否在购物车中存在
      const cartGoods = cartList.value.find(item => item.skuId === goods.skuId) // 判断当前添加的商品是否在购物车中存在
      if (cartGoods) {
        // 2.如果存在，修改商品数量即可
        cartGoods.count += goods.count
      } else {
        // 3.如果不存在，将商品添加到购物车中
        cartList.value.push(goods)
      }
    }
  }
  // 删除购物车商品逻辑
  const delCart = async (skuId) => {
    if(isLogin.value) {
      // 1.先判断当前用户是否登录
      await delCartAPI([skuId]) // 删除购物车商品
      getNewCartList()
    }
    // 1.根据skuId删除对应的商品
    // 根据skuId找到对应的商品索引，再使用splice删除
    const index = cartList.value.findIndex(item => item.skuId === skuId)
    cartList.value.splice(index, 1)
  }
  // 合并购物车
  const mergeCart = async () => {
    const mergeCartList = cartList.value.map(item => {
      return {
        skuId: item.skuId,
        selected: item.selected,
        count: item.count
      }
    })
    await mergeCartAPI(mergeCartList) // 合并购物车
    getNewCartList() // 获取最新购物车列表
  }
  // 清空购物车
  const clearCart = () => {
    cartList.value = []
  }
  // 单选功能
  const singleCheck = (skuId, selected) => {
    const cartGoods = cartList.value.find(item => item.skuId === skuId)
    cartGoods.selected = selected
  }
  // 全选功能
  const allCheck = (selected) => {
    // 1.循环遍历购物车列表，将商品选中状态修改为selected
    cartList.value.forEach(item => item.selected = selected)
  }
  // 3.定义计算属性，将购物车中商品的总数量进行展示
  const totalCount = computed(() => {
    return cartList.value.reduce((total, item) => total + item.count, 0)
  })
  const totalPrice = computed(() => {
    return cartList.value.reduce((total, item) => total + item.count * item.price, 0)
  })
  // 计算属性，全选的功能
  const isCheckedAll = computed(() => cartList.value.every(item => item.selected))
  // 定义计算属性，计算选中商品的数量和总价格
  const selectedCount = computed(() => {
    return cartList.value.filter(item => item.selected).reduce((total, item) => total + item.count, 0)
  })
  const selectedPrice = computed(() => {
    return cartList.value.filter(item => item.selected).reduce((total, item) => total + item.count * item.price, 0)
  })
  // 3.将state和action返回出去
  return {
    cartList,
    totalCount,
    totalPrice,
    isCheckedAll,
    getNewCartList,
    addCart,
    delCart,
    clearCart,
    mergeCart,
    singleCheck,
    allCheck,
    selectedCount,
    selectedPrice
  }
},{
  persist: true
})